package com.sunday.pinxinghui.model.order;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.adapter.CommonAdapter;
import com.sunday.pinxinghui.adapter.ViewHolder;
import com.sunday.pinxinghui.entity.ExpressInfo;
import com.sunday.pinxinghui.entity.Order;
import com.sunday.pinxinghui.network.ApiClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Author by Damon,  on 2017/12/1.
 */

public class PinxingLogisticsActivity extends BaseActivity {


    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.status)
    TextView status;
    @Bind(R.id.order_no)
    TextView orderNo;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    private Order order;
    private List<ExpressInfo> dataSet = new ArrayList<>();
    private CommonAdapter<ExpressInfo> adapter;
    private LinearLayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pingxing_logististics);
        ButterKnife.bind(this);
        titleView.setText("查看物流");
        order = (Order) getIntent().getSerializableExtra("order");
        status.setText("待收货");
        orderNo.setText(order.getOrderNo());
        initRecylerView();
        getData();

    }

    private void initRecylerView() {
        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new CommonAdapter<ExpressInfo>(mContext, R.layout.list_express_info_item, dataSet) {
            @Override
            public void convert(ViewHolder holder, ExpressInfo expressInfo, int position) {
                ImageView infoImg = holder.getView(R.id.info_img);
                infoImg.setImageResource(position == 0 ? R.mipmap.express_info2 : R.mipmap.express_info);
                TextView time = holder.getView(R.id.time);
                time.setText(expressInfo.getTime());
                TextView infoContent = holder.getView(R.id.info_content);
                infoContent.setText(expressInfo.getMemo());
                View view=holder.getView(R.id.view);
                view.setVisibility(position==dataSet.size()-1?View.GONE:View.VISIBLE);
            }
        };
        recyclerView.setAdapter(adapter);
    }

    private void getData() {
        showLoadingDialog(0);
        Call<ResultDO<List<ExpressInfo>>> call = ApiClient.getApiAdapter().getExpressInfo(order.getOrderId());
        call.enqueue(new Callback<ResultDO<List<ExpressInfo>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<ExpressInfo>>> call, Response<ResultDO<List<ExpressInfo>>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                dissMissDialog();
                if (response.body().getCode() == 0) {
                    if (response.body().getResult() == null) {
                        return;
                    }
                    dataSet.clear();
                    dataSet.addAll(response.body().getResult());
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<ExpressInfo>>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }


}
